<?php
  define ("HOST", "127.0.0.1");
  define ("PORT", 49999);

if ($_POST) {
  $username       = SafeStripSlashes ($_POST ["username"]);
  $password       = SafeStripSlashes ($_POST ["password"]);
  $news       = SafeStripSlashes ($_POST ["news"]);
} else {
	$username = "";
	$password = "";
	$news = "";
}
  function SafeStripSlashes ($buf)
  {
    return (get_magic_quotes_gpc ()) ? stripslashes ($buf) : $buf;
  }

  function PostNews ()
  {
    global $news, $username, $password;

	/* Create a TCP/IP socket. */
	$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
	if ($socket === false) {
		die("Socket creation failed");
	} 

	$result = socket_connect($socket, HOST, PORT);
	if ($result === false) {
		die("Could not connect to the host");
	}

	socket_write($socket, "Sending news\n");
	socket_write($socket, $username . "\n");
	socket_write($socket, $password . "\n");
	socket_write($socket, $news . "\n");
	socket_write($socket, "Done\n");
	
	$out = socket_read($socket, 2048, PHP_NORMAL_READ);
	print $out;

	socket_close($socket);
  }
  
  if ((strlen($username) > 0) && (strlen($password) > 0) && (strlen($news) > 0))
    PostNews ();
  else {
?>
<form action="/News.php" method="post" >
	Username: <input type="text" name="username" /><br />
	Password: <input type="password" name="password" /><br />
	News (one item per line):<br /><textarea rows="40" cols="80" name="news"></textarea><br />
	<input type="submit" />
</form>
<?php
}
?>